from django.db import models
from libs import ModelMixin, human_datetime
from apps.account.models import User


# Create your models here.
class MessageService(models.Model, ModelMixin):
    name = models.CharField("消息服务名称", max_length=50, unique=True, help_text="消息服务名称")
    webhook = models.CharField("消息服务webhook", max_length=200, help_text="消息服务webhook")
    type = models.CharField("消息服务类型", max_length=50, default="企业微信群机器人", help_text="消息服务类型")
    desc = models.CharField(max_length=255, null=True)

    created_at = models.CharField(max_length=20, default=human_datetime)
    created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
    deleted_at = models.CharField(max_length=20, null=True)
    deleted_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

    def __repr__(self):
        return '<MessageService %r>' % self.name

    class Meta:
        db_table = 'message_service'
        ordering = ('-id',)



class MessagePush(models.Model, ModelMixin):
    messageservice = models.ForeignKey(MessageService, on_delete=models.CASCADE, verbose_name="消息所属messageservice",
                                 help_text="消息所属messageservice")
    content = models.CharField("消息内容", max_length=500, help_text="消息内容", null=True)
    template = models.CharField("消息模板", max_length=500, help_text="消息模板", null=True)
    mentioned_list = models.CharField("@人员列表", max_length=50, help_text="@人员列表", null=True)
    desc = models.CharField(max_length=255, null=True)

    created_at = models.CharField(max_length=20, default=human_datetime)
    created_by = models.ForeignKey(User, models.PROTECT, related_name='+')

    def __repr__(self):
        return '<MessagePush %r>' % self.name

    class Meta:
        db_table = 'message_push'
        ordering = ('-id',)


class MessageTemplate(models.Model, ModelMixin):
    name = models.CharField(max_length=50)
    type = models.CharField(max_length=50)
    body = models.TextField()
    desc = models.CharField(max_length=255, null=True)

    created_at = models.CharField(max_length=20, default=human_datetime)
    created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
    updated_at = models.CharField(max_length=20, null=True)
    updated_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

    def __repr__(self):
        return '<MessageTemplate %r>' % self.name

    class Meta:
        db_table = 'message_template'
        ordering = ('-id',)


class MessageUser(models.Model, ModelMixin):
    name = models.CharField(max_length=50)
    userid = models.CharField(max_length=50)
    messageservice = models.ForeignKey(MessageService, models.PROTECT, related_name='+', null=True)

    created_at = models.CharField(max_length=20, default=human_datetime)
    created_by = models.ForeignKey(User, models.PROTECT, related_name='+')
    deleted_at = models.CharField(max_length=20, null=True)
    deleted_by = models.ForeignKey(User, models.PROTECT, related_name='+', null=True)

    def __repr__(self):
        return '<MessageUser %r>' % self.name

    class Meta:
        db_table = 'message_user'
        ordering = ('-id',)
